package com.wxl.common.wiget

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.wxl.common.R
import com.wxl.common.util.DPUtil

/**
 * create file time : 2021/3/26
 * create user : wxl
 * subscribe :
 */
class TabSelectorLayout : LinearLayout {

    private  var onTabSelectorItemClickListener:OnTabSelectorItemClickListener? = null

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        obtainAttr(context,attrs)
    }
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        obtainAttr(context,attrs)
    }


    private fun obtainAttr(context: Context?,attrs: AttributeSet?){
        orientation = HORIZONTAL
        gravity = Gravity.CENTER
        context?.let {
            val array = context.obtainStyledAttributes(attrs, R.styleable.TabSelectorLayout)
            val padding = array.getDimensionPixelSize(R.styleable.TabSelectorLayout_tabPadding, 1)
            val items = array.getString(R.styleable.TabSelectorLayout_tabItems)
            val tabSelectColor = array.getResourceId(R.styleable.TabSelectorLayout_tabSelectColor, android.R.color.darker_gray)
            val tabUnSelectColor = array.getResourceId(R.styleable.TabSelectorLayout_tabUnSelectColor, android.R.color.black)
            val tabSelectBg = array.getResourceId(
                R.styleable.TabSelectorLayout_tabSelectBg,
                R.drawable.shape_tab_selector_bg
            )
            val tabUnSelectBg = array.getResourceId(
                R.styleable.TabSelectorLayout_tabUnSelectBg,
                R.drawable.shape_tab_unselector_bg
            )
            val tabSelectStartBg = array.getResourceId(
                R.styleable.TabSelectorLayout_tabSelectStartBg,
                R.drawable.shape_tab_selector_start_bg
            )
            val tabSelectEndBg = array.getResourceId(
                R.styleable.TabSelectorLayout_tabSelectEndBg,
                R.drawable.shape_tab_selector_end_bg
            )
            val tabUnSelectStartBg = array.getResourceId(
                R.styleable.TabSelectorLayout_tabUnSelectStartBg,
                R.drawable.shape_tab_unselector_start_bg
            )
            val tabUnSelectEndBg = array.getResourceId(
                R.styleable.TabSelectorLayout_tabUnSelectEndBg,
                R.drawable.shape_tab_unselector_end_bg
            )

            val tabTextSize = array.getDimensionPixelSize(R.styleable.TabSelectorLayout_tabTextSize, 12)

            addItemView(items,padding, tabSelectColor, tabTextSize,
                tabUnSelectColor, tabSelectBg, tabUnSelectBg,
                tabSelectStartBg, tabSelectEndBg,
                tabUnSelectStartBg, tabUnSelectEndBg)
            array.recycle()
        }
    }


    private fun addItemView(items: String?,padding:Int,tabSelectColor:Int,tabTextSize:Int,
                               tabUnSelectColor:Int,tabSelectBg:Int,tabUnSelectBg:Int,
                               tabSelectStartBg:Int,tabSelectEndBg:Int,
                               tabUnSelectStartBg:Int,tabUnSelectEndBg:Int) {
        if(TextUtils.isEmpty(items)) return
        if(items!!.contains(",")){
            val split = items.split(",")
            for ((i,s) in split.withIndex()) {
                val textView = TextView(context)
                textView.gravity = Gravity.CENTER
                val params = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)
                val paddingPx = DPUtil.dip2px(padding)
                textView.text = s
                textView.textSize = tabTextSize.toFloat()
                textView.setPadding(DPUtil.dip2px(3),DPUtil.dip2px(2),DPUtil.dip2px(3),DPUtil.dip2px(2))
                when (i) {
                    0 -> {
                        textView.setTextColor(ContextCompat.getColor(context,tabSelectColor))
                        textView.setBackgroundResource(tabSelectStartBg)
                        params.topMargin = paddingPx
                        params.leftMargin = paddingPx
                        params.bottomMargin = paddingPx
                    }
                    split.size - 1 -> {
                        textView.setTextColor(ContextCompat.getColor(context,tabUnSelectColor))
                        textView.setBackgroundResource(tabUnSelectBg)
                        params.topMargin = paddingPx
                        params.rightMargin = paddingPx
                        params.bottomMargin = paddingPx
                    }
                    else -> {
                        textView.setTextColor(ContextCompat.getColor(context,tabUnSelectColor))
                        textView.setBackgroundResource(tabUnSelectEndBg)
                        params.topMargin = paddingPx
                        params.bottomMargin = paddingPx
                    }
                }
                textView.layoutParams = params
                textView.setOnClickListener {
                    onTabSelect(it,tabSelectColor, tabUnSelectColor, tabSelectBg, tabUnSelectBg,
                        tabSelectStartBg, tabSelectEndBg, tabUnSelectStartBg, tabUnSelectEndBg)
                }
                addView(textView)
            }
        } else {
            val textView = TextView(context)
            textView.gravity = Gravity.CENTER
            val params = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)
            textView.text = items
            textView.textSize = tabTextSize.toFloat()
            textView.setPadding(DPUtil.dip2px(3),DPUtil.dip2px(2),DPUtil.dip2px(3),DPUtil.dip2px(2))
            textView.layoutParams = params
            textView.setTextColor(ContextCompat.getColor(context,tabSelectColor))
            addView(textView)
        }

    }



    private fun onTabSelect(tab: View,tabSelectColor:Int,
                            tabUnSelectColor:Int,tabSelectBg:Int,tabUnSelectBg:Int,
                            tabSelectStartBg:Int,tabSelectEndBg:Int,
                            tabUnSelectStartBg:Int,tabUnSelectEndBg:Int){
        val i = indexOfChild(tab)
        onTabSelectorItemClickListener?.onTabSelectorItemClick(tab as TextView,i)
        for (p in 0 until childCount){
            val textView = getChildAt(p) as TextView
            if(i == p && i == 0){
                textView.setTextColor(ContextCompat.getColor(context,tabSelectColor))
                textView.setBackgroundResource(tabSelectStartBg)
            } else if(i == p && i == childCount - 1){
                textView.setTextColor(ContextCompat.getColor(context,tabSelectColor))
                textView.setBackgroundResource(tabSelectEndBg)
            } else if(i == p){
                textView.setTextColor(ContextCompat.getColor(context,tabSelectColor))
                textView.setBackgroundResource(tabSelectBg)
            } else if(i == 0){
                textView.setTextColor(ContextCompat.getColor(context,tabUnSelectColor))
                textView.setBackgroundResource(tabUnSelectStartBg)
            } else if (i == childCount - 1){
                textView.setTextColor(ContextCompat.getColor(context,tabUnSelectColor))
                textView.setBackgroundResource(tabUnSelectEndBg)
            } else {
                textView.setTextColor(ContextCompat.getColor(context,tabUnSelectColor))
                textView.setBackgroundResource(tabUnSelectBg)
            }
        }
    }

    companion object {
        interface OnTabSelectorItemClickListener{
            fun onTabSelectorItemClick(tab:TextView,position:Int)
        }
    }

    fun setOnTabSelectorItemClickListener(l:OnTabSelectorItemClickListener){
        this.onTabSelectorItemClickListener = l
    }
}